package com.jinxin.phonecloudmusic.myapplication.presenter.PresenterImpl;

import android.util.Log;

import com.jinxin.phonecloudmusic.myapplication.entity.GroupBean.BillboardList;
import com.jinxin.phonecloudmusic.myapplication.entity.GroupBean.Lyrics;
import com.jinxin.phonecloudmusic.myapplication.entity.GroupBean.Play;
import com.jinxin.phonecloudmusic.myapplication.entity.GroupBean.Search;
import com.jinxin.phonecloudmusic.myapplication.model.MainPageModel;
import com.jinxin.phonecloudmusic.myapplication.presenter.IPresenter.IMainPagePresenter;
import com.jinxin.phonecloudmusic.myapplication.view.IMainPageView;
import com.jinxin.phonecloudmusic.myapplication.view.IMusicImageFraView;
import com.jinxin.phonecloudmusic.myapplication.view.IMusicListView;
import com.jinxin.phonecloudmusic.myapplication.view.IMusicPlayView;
import com.jinxin.phonecloudmusic.myapplication.view.IMusicSearchView;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by ZJ on 2017/9/29 0029.
 */

public class IMainPagePresentImp implements IMainPagePresenter {
    private IMainPageView iMainPageView;//主页的视图
    private IMusicListView iMusicListView;//具体每类的音乐列表
    private IMusicPlayView iMusicPlayView;//
    private IMusicImageFraView musicImageFraView;
    private IMusicSearchView iMusicSearchView;

    private MainPageModel mainPageModel;

    public IMainPagePresentImp(IMainPageView iMainPageView) {
        this.iMainPageView = iMainPageView;
        mainPageModel=new MainPageModel();
        iMainPageView.jumb();
    }

    public IMainPagePresentImp(IMusicListView iMusicListView) {
        this.iMusicListView = iMusicListView;
        mainPageModel=new MainPageModel();
    }

    public IMainPagePresentImp(IMusicPlayView iMusicPlayView) {
        this.iMusicPlayView = iMusicPlayView;
        mainPageModel=new MainPageModel();
    }

    public IMainPagePresentImp(IMusicImageFraView musicImageFraView) {
        this.musicImageFraView = musicImageFraView;
        mainPageModel=new MainPageModel();
    }

    public IMainPagePresentImp(IMusicSearchView iMusicSearchView) {
        this.iMusicSearchView = iMusicSearchView;
        mainPageModel=new MainPageModel();
    }

    //获取榜单
    @Override
    public void allBillboardList(final String method, final int type, final int size, final int offset) {
        Observable.create(new ObservableOnSubscribe<BillboardList>() {
            @Override
            public void subscribe(ObservableEmitter<BillboardList> e) throws Exception {
                e.onNext(mainPageModel.billboardList(method,type,size,offset));
                e.onComplete();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<BillboardList>() {
            @Override
            public void accept(BillboardList s) throws Exception {
                if (null!=iMainPageView)
                iMainPageView.jumbMusicLIstDetail(s);
                Log.e("----加载数据","加载数据");
            }
        });
    }

    //获取每类音乐的榜单
    @Override
    public void BillboardList(final String method, final int type, final int size, final int offset) {
        Observable.create(new ObservableOnSubscribe<BillboardList>() {
            @Override
            public void subscribe(ObservableEmitter<BillboardList> e) throws Exception {
                e.onNext(mainPageModel.billboardList(method,type,size,offset));
                e.onComplete();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<BillboardList>() {
            @Override
            public void accept(BillboardList s) throws Exception {
                if (null!=iMusicListView)
                iMusicListView.setListView(s);
                Log.e("----加载数据","加载数据");
            }
        });
    }

    //播放音乐
    @Override
    public void playMusic(final String method, final String songid) {
        Observable.create(new ObservableOnSubscribe<Play>() {
            @Override
            public void subscribe(ObservableEmitter<Play> e) throws Exception {
                e.onNext(mainPageModel.playMusic(method,songid));
                e.onComplete();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Play>() {
            @Override
            public void accept(Play s) throws Exception {
                if (null!=iMusicPlayView)
                iMusicPlayView.updatePlayState(s);


            }
        });
    }

    @Override
    public void fraPlayMusic(final String method, final String songid) {
        Observable.create(new ObservableOnSubscribe<Play>() {
            @Override
            public void subscribe(ObservableEmitter<Play> e) throws Exception {
                e.onNext(mainPageModel.playMusic(method,songid));
                e.onComplete();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Play>() {
            @Override
            public void accept(Play s) throws Exception {
                if (null!=musicImageFraView)
                musicImageFraView.updateView(s);

            }
        });
    }

    //获取歌词
    @Override
    public void playMusicLric(final String method, final String songid) {
        Observable.create(new ObservableOnSubscribe<Lyrics>() {
            @Override
            public void subscribe(ObservableEmitter<Lyrics> e) throws Exception {
                e.onNext(mainPageModel.lyrics(method,songid));
                e.onComplete();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Lyrics>() {
            @Override
            public void accept(Lyrics s) throws Exception {
                if (null!=iMusicPlayView)
                iMusicPlayView.updateLric(s);
            }
        });
    }

    //搜索
    @Override
    public void search(final String method, final String query) {
        Observable.create(new ObservableOnSubscribe<Search>() {
            @Override
            public void subscribe(ObservableEmitter<Search> e) throws Exception {
                e.onNext(mainPageModel.search(method,query));
                e.onComplete();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Search>() {
            @Override
            public void accept(Search s) throws Exception {
                if (null!=iMusicSearchView)
                iMusicSearchView.getSongInfo(s);
            }
        });
    }





}
